package org.jivesoftware.smackx.search;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.packet.DataForm;
import org.xmlpull.v1.XmlPullParser;

public class UserSearch extends IQ
{
  private static void buildDataForm(SimpleUserSearch paramSimpleUserSearch, String paramString, XmlPullParser paramXmlPullParser)
    throws Exception
  {
    DataForm localDataForm = new DataForm("form");
    int i = 0;
    localDataForm.setTitle("User Search");
    localDataForm.addInstruction(paramString);
    while (true)
    {
      int j;
      label212: 
      do
        while (true)
        {
          do
          {
            if (i != 0)
            {
              if (paramSimpleUserSearch.getExtension("x", "jabber:x:data") == null)
                paramSimpleUserSearch.addExtension(localDataForm);
              return;
            }
            j = paramXmlPullParser.next();
            if ((j == 2) && (!(paramXmlPullParser.getNamespace().equals("jabber:x:data"))))
            {
              String str = paramXmlPullParser.getName();
              FormField localFormField = new FormField(str);
              if (str.equals("first"))
                localFormField.setLabel("First Name");
              while (true)
              {
                do
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        localFormField.setType("text-single");
                        localDataForm.addField(localFormField);
                      }
                      if (!(str.equals("last")))
                        break;
                      localFormField.setLabel("Last Name");
                    }
                    if (!(str.equals("email")))
                      break;
                    localFormField.setLabel("Email Address");
                  }
                while (!(str.equals("nick")));
                localFormField.setLabel("Nickname");
              }
            }
            if (j != 3)
              break label212;
          }
          while (!(paramXmlPullParser.getName().equals("query")));
          i = 1;
        }
      while ((j != 2) || (!(paramXmlPullParser.getNamespace().equals("jabber:x:data"))));
      paramSimpleUserSearch.addExtension(PacketParserUtils.parsePacketExtension(paramXmlPullParser.getName(), paramXmlPullParser.getNamespace(), paramXmlPullParser));
      i = 1;
    }
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<query xmlns=\"jabber:iq:search\">");
    localStringBuilder.append(getExtensionsXML());
    localStringBuilder.append("</query>");
    return localStringBuilder.toString();
  }

  public Form getSearchForm(Connection paramConnection, String paramString)
    throws XMPPException
  {
    UserSearch localUserSearch = new UserSearch();
    localUserSearch.setType(IQ.Type.GET);
    localUserSearch.setTo(paramString);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localUserSearch.getPacketID()));
    paramConnection.sendPacket(localUserSearch);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server on status set.");
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
    return Form.getFormFrom(localIQ);
  }

  public ReportedData sendSearchForm(Connection paramConnection, Form paramForm, String paramString)
    throws XMPPException
  {
    ReportedData localReportedData;
    UserSearch localUserSearch = new UserSearch();
    localUserSearch.setType(IQ.Type.SET);
    localUserSearch.setTo(paramString);
    localUserSearch.addExtension(paramForm.getDataFormToSend());
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localUserSearch.getPacketID()));
    paramConnection.sendPacket(localUserSearch);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server on status set.");
    if (localIQ.getError() != null)
      localReportedData = sendSimpleSearchForm(paramConnection, paramForm, paramString);
    while (true)
    {
      return localReportedData;
      localReportedData = ReportedData.getReportedDataFrom(localIQ);
    }
  }

  public ReportedData sendSimpleSearchForm(Connection paramConnection, Form paramForm, String paramString)
    throws XMPPException
  {
    ReportedData localReportedData;
    SimpleUserSearch localSimpleUserSearch = new SimpleUserSearch();
    localSimpleUserSearch.setForm(paramForm);
    localSimpleUserSearch.setType(IQ.Type.SET);
    localSimpleUserSearch.setTo(paramString);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localSimpleUserSearch.getPacketID()));
    paramConnection.sendPacket(localSimpleUserSearch);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server on status set.");
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
    if (localIQ instanceof SimpleUserSearch)
      localReportedData = ((SimpleUserSearch)localIQ).getReportedData();
    while (true)
    {
      return localReportedData;
      localReportedData = null;
    }
  }

  public static class Provider
  implements IQProvider
  {
    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      Object localObject;
      UserSearch localUserSearch = null;
      SimpleUserSearch localSimpleUserSearch = new SimpleUserSearch();
      int i = 0;
      if (i != 0)
      {
        if (localUserSearch == null)
          break label181;
        localObject = localUserSearch;
      }
      while (true)
      {
        while (true)
        {
          int j;
          do
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  return localObject;
                  j = paramXmlPullParser.next();
                  if ((j != 2) || (!(paramXmlPullParser.getName().equals("instructions"))))
                    break;
                  UserSearch.access$0(localSimpleUserSearch, paramXmlPullParser.nextText(), paramXmlPullParser);
                  localObject = localSimpleUserSearch;
                }
                if ((j != 2) || (!(paramXmlPullParser.getName().equals("item"))))
                  break;
                localSimpleUserSearch.parseItems(paramXmlPullParser);
                localObject = localSimpleUserSearch;
              }
              if ((j != 2) || (!(paramXmlPullParser.getNamespace().equals("jabber:x:data"))))
                break;
              localUserSearch = new UserSearch();
              localUserSearch.addExtension(PacketParserUtils.parsePacketExtension(paramXmlPullParser.getName(), paramXmlPullParser.getNamespace(), paramXmlPullParser));
            }
          while ((j != 3) || (!(paramXmlPullParser.getName().equals("query"))));
          i = 1;
        }
        label181: localObject = localSimpleUserSearch;
      }
    }
  }
}